package com.tanhua.service;

import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Friend;
import com.tanhua.domain.vo.ContactVO;
import com.tanhua.domain.vo.MessageCommentVO;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.template.HuanXinTemplate;
import com.tanhua.util.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MessageService {
    @Reference
    private FriendApi friendApi;

    @Reference
    private UserInfoApi userInfoApi;

    @Reference
    private CommentApi commentApi;


    @Autowired
    private HuanXinTemplate huanXinTemplate;


    public ResponseEntity contacts(Long targetUserId) {
        User user = UserHolder.getUser();
        friendApi.add(targetUserId,user.getId());
        huanXinTemplate.addContact(user.getId().toString(),targetUserId.toString());

        return ResponseEntity.ok(null);
    }

    public ResponseEntity people(Integer page, Integer pagesize) {
        User user = UserHolder.getUser();
        PageResult pageResult = friendApi.people(user.getId(),page,pagesize);
        List<Friend> items = pageResult.getItems();
        ArrayList<ContactVO> vos = new ArrayList<>();
        for (Friend friend : items) {
            ContactVO vo = new ContactVO();
            UserInfo userInfo = userInfoApi.findUserInfo(friend.getFriendId());
            BeanUtils.copyProperties(userInfo,vo);
            vo.setId(user.getId());
            vo.setUserId(friend.getFriendId().toString());
            vos.add(vo);
        }
        pageResult.setItems(vos);
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity likeList(Integer page, Integer pagesize, int commentType) {
        User user = UserHolder.getUser();
        PageResult pageResult = commentApi.commentList(page,pagesize,user.getId(),commentType);
        ArrayList<MessageCommentVO> vos = new ArrayList<>();
        List<Comment> items = pageResult.getItems();
        for (Comment comment : items) {
            MessageCommentVO vo = new MessageCommentVO();
            UserInfo userInfo = userInfoApi.findUserInfo(comment.getUserId());
            BeanUtils.copyProperties(userInfo, vo);
            vo.setId(userInfo.getId().toString());
            vo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(comment.getCreated())));
            vos.add(vo);
        }
        pageResult.setItems(vos);
        System.out.println(pageResult);
        return ResponseEntity.ok(pageResult);
    }
}
